package com.ruoyi.basebill.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.basebill.DTO.QqjffEntryInfo;
import com.ruoyi.basebill.DTO.QqjffInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.basebill.domain.WfzFfqqjentry;
import com.ruoyi.basebill.mapper.WfzFfqqjMapper;
import com.ruoyi.basebill.domain.WfzFfqqj;
import com.ruoyi.basebill.service.IWfzFfqqjService;

/**
 * 发放全勤奖Service业务层处理
 * 
 * @author xiefeng
 * @date 2022-12-19
 */
@Service
public class WfzFfqqjServiceImpl implements IWfzFfqqjService 
{
    @Autowired
    private WfzFfqqjMapper wfzFfqqjMapper;

    /**
     * 查询发放全勤奖
     * 
     * @param fid 发放全勤奖主键
     * @return 发放全勤奖
     */
    @Override
    public WfzFfqqj selectWfzFfqqjByFid(Long fid)
    {
        return wfzFfqqjMapper.selectWfzFfqqjByFid(fid);
    }

    /**
     * 查询发放全勤奖列表
     * 
     * @param wfzFfqqj 发放全勤奖
     * @return 发放全勤奖
     */
    @Override
    public List<WfzFfqqj> selectWfzFfqqjList(WfzFfqqj wfzFfqqj)
    {
        return wfzFfqqjMapper.selectWfzFfqqjList(wfzFfqqj);
    }

    /**
     * 新增发放全勤奖
     * 
     * @param wfzFfqqj 发放全勤奖
     * @return 结果
     */
    @Transactional
    @Override
    public int insertWfzFfqqj(WfzFfqqj wfzFfqqj)
    {
        int rows = wfzFfqqjMapper.insertWfzFfqqj(wfzFfqqj);
        insertWfzFfqqjentry(wfzFfqqj);
        return rows;
    }

    /**
     * 修改发放全勤奖
     * 
     * @param wfzFfqqj 发放全勤奖
     * @return 结果
     */
    @Transactional
    @Override
    public int updateWfzFfqqj(WfzFfqqj wfzFfqqj)
    {
        wfzFfqqjMapper.deleteWfzFfqqjentryByFparentid(wfzFfqqj.getFid());
        insertWfzFfqqjentry(wfzFfqqj);
        return wfzFfqqjMapper.updateWfzFfqqj(wfzFfqqj);
    }

    /**
     * 批量删除发放全勤奖
     * 
     * @param fids 需要删除的发放全勤奖主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWfzFfqqjByFids(Long[] fids)
    {
        wfzFfqqjMapper.deleteWfzFfqqjentryByFparentids(fids);
        return wfzFfqqjMapper.deleteWfzFfqqjByFids(fids);
    }

    /**
     * 删除发放全勤奖信息
     * 
     * @param fid 发放全勤奖主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWfzFfqqjByFid(Long fid)
    {
        wfzFfqqjMapper.deleteWfzFfqqjentryByFparentid(fid);
        return wfzFfqqjMapper.deleteWfzFfqqjByFid(fid);
    }

    /**
     * 新增发放全勤奖分录信息
     * 
     * @param wfzFfqqj 发放全勤奖对象
     */
    public void insertWfzFfqqjentry(WfzFfqqj wfzFfqqj)
    {
        List<WfzFfqqjentry> wfzFfqqjentryList = wfzFfqqj.getWfzFfqqjentryList();
        Long fid = wfzFfqqj.getFid();
        if (StringUtils.isNotNull(wfzFfqqjentryList))
        {
            List<WfzFfqqjentry> list = new ArrayList<WfzFfqqjentry>();
            for (WfzFfqqjentry wfzFfqqjentry : wfzFfqqjentryList)
            {
                wfzFfqqjentry.setFparentid(fid);
                list.add(wfzFfqqjentry);
            }
            if (list.size() > 0)
            {
                wfzFfqqjMapper.batchWfzFfqqjentry(list);
            }
        }
    }

    /**
     * 导入全勤奖发放
     */
    @Override
    public String importQqjff(WfzFfqqj wfzFfqqj) {
        QqjffInfo qqjffInfo = new QqjffInfo();
        String fnumber = wfzFfqqj.getFnumber();//单据编号
        String fcompany = wfzFfqqj.getFcompany();//组织（公司）
        String fperiod = wfzFfqqj.getFperiod();//薪酬期间
        String fvourtype = wfzFfqqj.getFvourtype();//凭证字

        qqjffInfo.setNumber(fnumber);
        qqjffInfo.setCompany(fcompany);
        qqjffInfo.setPeriod(fperiod);
        qqjffInfo.setVourType(fvourtype);
        qqjffInfo.setSourcebillid(String.valueOf(wfzFfqqj.getFid()));

        List<WfzFfqqjentry> wfzFfqqjentryList = wfzFfqqj.getWfzFfqqjentryList();
        ArrayList<QqjffEntryInfo> entry = new ArrayList<>();
        //组装分录数据
        for (int i = 0; i < wfzFfqqjentryList.size(); i++) {
            WfzFfqqjentry wfzFfqqjentry = wfzFfqqjentryList.get(i);
            String fcostnumber = wfzFfqqjentry.getFcostnumber();//成本中心
            BigDecimal fqqj = wfzFfqqjentry.getFqqj();//全勤奖
            String fcostcompany = wfzFfqqjentry.getFcostcompany();//成本中心所属财务组织
            String fjzcompany = wfzFfqqjentry.getFjzcompany();//记账公司
            String fjsfs = wfzFfqqjentry.getFjsfs();//组织间结算方式
            String fcbzxxz = wfzFfqqjentry.getFcbzxxz();//成本中心属性
            String fvourtype1 = wfzFfqqjentry.getFvourtype();//凭证字

            QqjffEntryInfo qqjffEntryInfo = new QqjffEntryInfo();
            qqjffEntryInfo.setCostCenter(fcostnumber);
            qqjffEntryInfo.setQqj(fqqj);
            qqjffEntryInfo.setCostcompany(fcostcompany);
            qqjffEntryInfo.setJzcompany(fjzcompany);
            qqjffEntryInfo.setJsfs(fjsfs);
            qqjffEntryInfo.setCbzxxz(fcbzxxz);
            qqjffEntryInfo.setVourType(fvourtype1);

            entry.add(qqjffEntryInfo);
        }

        qqjffInfo.setEntry(entry);

        //转为Json
        String json = JSONObject.toJSONString(qqjffInfo);
        return json;
    }
}
